package com.ruoyi.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.ZrsMbManageGoal;
import com.ruoyi.business.domain.mbglxzb.MbGlxYjzb;
import com.ruoyi.business.service.IZrsMbManageGoalService;
import com.ruoyi.business.service.IZrsMbManageService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 目标项管理记录Controller
 * 
 * @author Yuhf
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/business/ZrsMbManageGoal")
public class ZrsMbManageGoalController extends BaseController
{
    @Autowired
    private IZrsMbManageGoalService zrsMbManageGoalService;
    @Autowired
    private IZrsMbManageService zrsMbManageService;

    /**
     * 查询目标项管理记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZrsMbManageGoal zrsMbManageGoal)
    {
        startPage();
        List<ZrsMbManageGoal> list = zrsMbManageGoalService.selectZrsMbManageGoalList(zrsMbManageGoal);
        return getDataTable(list);
    }
    /**
     * 查询目标项管理记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:list')")
    @GetMapping("/listData")
    public AjaxResult listData(ZrsMbManageGoal zrsMbManageGoal)
    {
    	//查询模版 管理项对应的 指标等
        List<ZrsMbManageGoal> list = zrsMbManageGoalService.selectZrsMbManageGoalList(zrsMbManageGoal);
        Map<String, Object> date = new HashMap<String, Object>();
        for (ZrsMbManageGoal zrsMbManageGoal2 : list) {
        	zrsMbManageGoal.setMbGoalId(zrsMbManageGoal2.getMbGoalId());//管理项id
        	List<MbGlxYjzb> mbGlxZbList = zrsMbManageGoalService.selectZrsMbManageGoalZbListObj(zrsMbManageGoal);
        	if(!mbGlxZbList.isEmpty()) {
        		date.put("glx_zblist_"+zrsMbManageGoal2.getMbGoalId().toString(), mbGlxZbList);
        	}
		}
        date.put("mbGlxList", list);
        date.put("zrsMbInfo", zrsMbManageService.selectZrsMbManageByMbId(zrsMbManageGoal.getMbId()));
        
        //查询所有的  后勤部门进行考核
        date.put("shDeptInfo", zrsMbManageService.selectZbManageShDeptAll());
        
        return AjaxResult.success(date);
    }
    /**
     * 导出目标项管理记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:export')")
    @Log(title = "目标项管理记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZrsMbManageGoal zrsMbManageGoal)
    {
        List<ZrsMbManageGoal> list = zrsMbManageGoalService.selectZrsMbManageGoalList(zrsMbManageGoal);
        ExcelUtil<ZrsMbManageGoal> util = new ExcelUtil<ZrsMbManageGoal>(ZrsMbManageGoal.class);
        util.exportExcel(response, list, "目标项管理记录数据");
    }

    /**
     * 获取目标项管理记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:query')")
    @GetMapping(value = "/{mbGoalId}")
    public AjaxResult getInfo(@PathVariable("mbGoalId") Long mbGoalId)
    {
        return success(zrsMbManageGoalService.selectZrsMbManageGoalByMbGoalId(mbGoalId));
    }
    
    
    /**
     * 选择模版目标项管理记录
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:list')")
    @PostMapping(value = "/getZrsMbManageGoalSelZb")
    public AjaxResult getZrsMbManageGoalSelZb(@RequestBody ZrsMbManageGoal zrsMbManageGoal)
    {
    	//zrsMbManageGoalService.updateZrsMbManageGoal(zrsMbManageGoal)
    	//首先选择了那些  管理项指标
    	Map<String,Object> date = new HashMap<String,Object>();
    	date.put("zbData", zrsMbManageGoalService.selectZbManageListByMbId(zrsMbManageGoal));
    	date.put("selGlxZbVal", zrsMbManageGoalService.selectSelZbManageListByMbId(zrsMbManageGoal));
    	
        return AjaxResult.success(date);
    }
    
    /**
     * 新增目标项管理记录
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:add')")
    @Log(title = "目标项管理记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZrsMbManageGoal zrsMbManageGoal)
    {
    	zrsMbManageGoal.setCreateTime(DateUtils.getNowDate());
    	zrsMbManageGoal.setCreateBy(getUsername());
        return toAjax(zrsMbManageGoalService.insertZrsMbManageGoal(zrsMbManageGoal));
    }

    /**
     * 修改目标项管理记录
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:edit')")
    @Log(title = "目标项管理记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZrsMbManageGoal zrsMbManageGoal)
    {
    	
    	zrsMbManageGoal.setUpdateTime(DateUtils.getNowDate());
    	zrsMbManageGoal.setUpdateBy(getUsername());
        return toAjax(zrsMbManageGoalService.updateZrsMbManageGoal(zrsMbManageGoal));
    }

    /**
     * 删除目标项管理记录
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManageGoal:remove')")
    @Log(title = "目标项管理记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mbGoalId}")
    public AjaxResult remove(@PathVariable Long mbGoalId)
    {
    	ZrsMbManageGoal zrsMbManageGoal = new ZrsMbManageGoal();
    	zrsMbManageGoal.setDelTime(DateUtils.getNowDate());
    	zrsMbManageGoal.setDelBy(getUsername());
    	zrsMbManageGoal.setMbGoalId(mbGoalId);
        return toAjax(zrsMbManageGoalService.updateZrsMbManageGoal(zrsMbManageGoal));
    }
}
